package com.conytech.conydishm;

import java.util.ArrayList;

import com.fedorvlasov.lazylist.ImageLoader;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;


public class MyAdapter extends ArrayAdapter<ClientItem> {

	private final Context context;
	private final ArrayList<ClientItem> itemsArrayList;
	private LayoutInflater inflater;
	public ImageLoader imageLoader;

	public MyAdapter(Context context, ArrayList<ClientItem> itemsArrayList) {
		super(context, R.layout.row, itemsArrayList);
		this.context = context;
		this.itemsArrayList = itemsArrayList;

		imageLoader=new ImageLoader(context.getApplicationContext());

		// 1. Create inflater
		inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}


	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		// 2. Get rowView from inflater
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.row, parent, false);

			//convertView.setFocusable(false);
		}

		// 3. Get the two text view from the rowView
		ImageView clientImg = (ImageView) convertView.findViewById(R.id.lv_client_image);
		TextView clientCodeLabel = (TextView) convertView.findViewById(R.id.lv_client_code);
		TextView clientNameLabel = (TextView) convertView.findViewById(R.id.lv_client_name);
		Button orderCallBtn = (Button) convertView.findViewById(R.id.lv_button_order_call);
		Button immPayBtn = (Button) convertView.findViewById(R.id.lv_imm_pay);

		// 4. Set the text for textView
		clientCodeLabel.setText(itemsArrayList.get(position).getClient_code());
		clientNameLabel.setText(itemsArrayList.get(position).getClient_name());

		//버튼으로 인한 리스트 뷰 클릭 문제 해결
		orderCallBtn.setFocusable(false);
		immPayBtn.setFocusable(false);

		final int pos = position;
		// 이벤트
		// 이미지 주소
		imageLoader.DisplayImage("http://192.168.11.38/client_img/" +  itemsArrayList.get(pos).getClient_code() + ".png", clientImg);
		if( itemsArrayList.get(position).getImg_yn().equals("Y") ){
			clientImg.setVisibility(View.VISIBLE);
		} else {
			clientImg.setVisibility(View.GONE);
		}

		// 전화걸기
		orderCallBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent in = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + itemsArrayList.get(pos).getTel_no()));
				context.startActivity(in);
			}
		});

		// 5. retrn rowView
		return convertView;

	}

}